package com.un.ebs.core.tenant;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.un.ebs.core.UserContext;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AuditMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        boolean createDate = metaObject.hasSetter("createTime");
        boolean updateDate = metaObject.hasSetter("lastModifiedTime");
        boolean lastUpdateDate = metaObject.hasSetter("lastUpdateTime");
        if (createDate || updateDate) {
            Date now = new Date();
            if (createDate) {
                this.setFieldValByName("createTime", now, metaObject);
            }
            if (updateDate) {
                this.setFieldValByName("lastModifiedTime", now, metaObject);
            }
            if (lastUpdateDate) {
                this.setFieldValByName("lastUpdateTime", now, metaObject);
            }
        }
        try {
            String userId = SecurityUtils.getUserContent().getId();
            String userName = StringUtils.isNullOrEmpty(SecurityUtils.getUserContent().getPersonnelName()) ? SecurityUtils.getUserContent().getUserName() : SecurityUtils.getUserContent().getPersonnelName();
            if (metaObject.hasSetter("createAt")) {
                this.setFieldValByName("createAt", userId, metaObject);
            }
            if (metaObject.hasSetter("createAtName")) {
                this.setFieldValByName("createAtName", userName, metaObject);
            }
            if (metaObject.hasSetter("lastModifiedAt")) {
                this.setFieldValByName("lastModifiedAt", userId, metaObject);
            }
            if (metaObject.hasSetter("lastModifiedAtName")) {
                this.setFieldValByName("lastModifiedAtName", userName, metaObject);
            }
        } catch (Exception e) {
            return;
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {

        if (metaObject.hasSetter("lastModifiedTime")) {
            this.setFieldValByName("lastModifiedTime", new Date(), metaObject);
        }
        if (metaObject.hasSetter("lastUpdateTime")) {
            this.setFieldValByName("lastUpdateTime", new Date(), metaObject);
        }

        UserContext userContext = null;
        try {
            userContext = SecurityUtils.getUserContent();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }

        if (metaObject.hasSetter("lastModifiedAt")) {
            String userId = userContext.getId();
            this.setFieldValByName("lastModifiedAt", userId, metaObject);
        }
        if (metaObject.hasSetter("lastModifiedAtName")) {
            String userName = userContext.getId();
            this.setFieldValByName("lastModifiedAtName", userName, metaObject);
        }
    }
}
